package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.dao.LogDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.query.QueryGoods;
import com.atguigu.jxc.log.LogType;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsTypeDao goodsTypeDao;
    @Autowired
    LogService logService;

    @Override
    public Map<String, Object> listAlarm() {
        List<Goods> list=goodsDao.listAlarm();
        Map<String ,Object> map=new HashMap<>();
        map.put("rows",list);
        return map;
    }

    @Override
    public Boolean updateInventory(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {

        Integer rows=goodsDao.updateInventory(goodsId,inventoryQuantity,purchasingPrice);
        if(rows>0){
            return true;
        }
        return false;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Integer size=(page-1)*rows;
        List<Goods> list = goodsDao.findPageByQueryGoods(size, rows, nameOrCode, null,"hasInventory");
        Map<String ,Object> map=new HashMap<>();
        Integer total=goodsDao.getTotal(nameOrCode,null,"hasInventory");
        map.put("total",total);
        map.put("rows",list);
        return map;
    }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Integer size=(page-1)*rows;
        List<Goods> list = goodsDao.findPageByQueryGoods(size, rows, nameOrCode, null,"notInventory");
        Map<String ,Object> map=new HashMap<>();
        Integer total=goodsDao.getTotal(nameOrCode,null,"notInventory");
        map.put("total",total);
        map.put("rows",list);
        return map;
    }
    @com.atguigu.jxc.log.Log(value = "删除商品信息"+"#{#args[0]}",logType = LogType.DELETE_ACTION)
    @Override
    public Boolean delete(Integer goodsId) {
        Integer row=goodsDao.delete(goodsId);
        if(row>0){
            return true;
        }
        return false;
    }
    @Transactional
    @Override
    public void saveOrUpdate(Integer goodsId, Goods goods) {
        if(goodsId!=null){
            goodsDao.update(goods);

            logService.save(new Log(Log.UPDATE_ACTION,"修改商品信息"));
        }else {
            goods.setInventoryQuantity(0);
            goods.setState(0);
            goodsDao.save(goods);
            logService.save(new Log(Log.INSERT_ACTION,"添加商品信息"));
        }
    }

    @Override
    public Map<String, Object> findPageByQueryGoods(QueryGoods queryGoods) {
        Integer page = queryGoods.getPage();
        Integer limit = queryGoods.getRows();
        Integer size=(page-1)*limit;
        String codeOrName = queryGoods.getCodeOrName();

        Integer goodsTypeId = queryGoods.getGoodsTypeId();
        List<Goods> list=null;
        Integer total=null;
        if(goodsTypeId!=null){
            List<Integer> goodsTypeIds = goodsTypeDao.getChildrenId(goodsTypeId);
            if(goodsTypeIds!=null&&goodsTypeIds.size()==0){
                goodsTypeIds.add(goodsTypeId);
            }
            list=goodsDao.findPageByQueryGoods(size,limit,codeOrName,goodsTypeIds.toArray(),null);
            total=goodsDao.getTotal(codeOrName,goodsTypeIds.toArray(),null);
        }else {
            list=goodsDao.findPageByQueryGoods(size,limit,codeOrName,null,null);
            total=goodsDao.getTotal(codeOrName,null,null);
        }
        Map<String ,Object> map=new HashMap<>();
        map.put("rows",list);

        map.put("total",total);
        return map;
    }

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }


}
